package poketrade.managers;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import poketrade.entities.Carte;

@Stateless
public class CardsManager {

	@PersistenceContext(name="Poketrade")
	EntityManager em;
	
	public Carte registerCard(Carte c)
	{
		if (getCardById(c.getCardIndex()) != null)
			return null;
		em.persist(c);
		return c;
	}
	
	public Carte getCardById(String cardIdentifier)
	{
		return em.find(Carte.class, cardIdentifier);
	}
	
	@SuppressWarnings("unchecked")
	public List<Carte> getCartes()
	{
		return em.createNamedQuery("getAllCartes").getResultList();
	}
	
	public void removeCard(String cardIdentifier)
	{
		em.remove(em.find(Carte.class, cardIdentifier));
	}
}
